//
//  AppDelegate.swift
//  新浪微博-初建项目
//
//  Created by danny on 15/6/24.
//  Copyright © 2015年 DannyLiu. All rights reserved.
//

import UIKit
import AFNetworking

 /// 全局变量 记录当前用户信息
var sharedUserAccount = UserAccount.loadAccount()
/// 切换根控制器的通知
let HMSwitchRootVCNotification = "HMSwitchRootVCNotification"
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // 先实例化全局的network工具对象
        NetWorkingTools.sharedNetWorkingTools()
        
        // 注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchViewController:", name: HMSwitchRootVCNotification, object: nil)
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        //TODO: warning 登录测试选项
        window?.rootViewController = MainViewController()
        window?.makeKeyAndVisible()
        
        // 调用设置全局外观的方法
        setAppearance()
        
        // 设置网络
        setupNetwork()
        
        return true

    }
    
    private func defaultViewController() -> UIViewController {
        
        // 1 判断用户是否登录
        if sharedUserAccount != nil {
            
            // 2 如果登录,判断是否有新的版本
            return isNewUpdate() ? NewFeatureViewController() : WelcomeViewController()
        }
        return MainViewController()
        
    }
    ///  切换控制器
    func switchViewController(n: NSNotification) {
        // 判断 object 是否为 true，如果是，显示 MainViewController
        let isMainVC = n.object as! Bool
        
        print("\(isMainVC)--------------------------")
        
        
        window?.rootViewController = isMainVC ? MainViewController() : WelcomeViewController()
    }
    
    
    /// 是否新版本
    private func isNewUpdate() -> Bool {
        
        // 1. 获取应用程序`当前版本`
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let version = NSNumberFormatter().numberFromString(currentVersion)!.doubleValue
        print("当前的版本是\(version)")
        
        // 2. 获取应用程序`之前的版本`，从用户偏好中读取
        let versionKey = "versionKey"
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(versionKey)
//        print(sandBoxVersion)
        
        // 3. 将`当前版本`写入用户偏好
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: versionKey)
        
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\(version > sandBoxVersion)")
        
        return version > sandBoxVersion
    }
    
    

    ///  设置网络
    private func setupNetwork(){
        // 设置网络指示器
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        
        // 设置网络缓存
        let urlCache = NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil)
        
        NSURLCache.setSharedURLCache(urlCache)
        
        
    }
    
    // 设置全局外观
    func setAppearance(){
        
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
    }
    
}

